<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>e网批发管理后台</title>
  <?php 
include("common/conn.php");
include('./tool/UpFileTool.class.php');
if($_SESSION['shopid']==""){
  echo "<script>window.location.href='login.php';</script>";
} 

// 获取当前时间
date_default_timezone_set('Asia/Shanghai');
$time = date("Y-m-d H:i:s");
// 获取当前时间 end
  if($_GET['del']=="ok")
  {
    $area = array(); 
    $area= $_POST['checkbox'];

    foreach ($area as $k=>$v)
    {
      $srat1=mysql_query("DELETE FROM goodsorders where ordersid='".$v."'");
      $srat1=mysql_query("DELETE FROM car where ordersid='".$v."'");
    }

  }

  if(isset($_POST[kuaidi]))
  {
    $state=2;
    $srat=mysql_query("update goodsorders set kuaidi='".$_POST[kuaidi]."',danhao='".$_POST[danhao]."',state='".$state."' where ordersid='".$_POST[ordersid]."'");

    // 调用信息推送接口
    $url = "http://120.24.0.10/xzpf/api/jpush/jpush.php?state=".$state."&ordersid=".$_POST[ordersid];
    file_get_contents($url);
    // 调用信息推送接口 end
    // 查找订单自动确认收货时间 
    $query = mysql_query("select confirm_time from categroy_type where id=(select categroy_type_id from categroy where cateid=(select leimu from shop where shopid=(select shopid from goodsorders where ordersid=".$_POST[ordersid].")))");
    $categroy_type = mysql_fetch_assoc($query);
    $confirm_time = $categroy_type[confirm_time];
    // 查找订单自动确认收货时间 end
    // 定时器
    $url = "http://120.24.0.10/xzpf/changeAfterTime/changeOrderState.php?ordersid=".$_POST[ordersid]."";
    $start_date = date("Y-m-d",strtotime(''.$confirm_time.'days'));
    $start_hour = date("H");
    $start_minute = date("i");
    $tag = 'setOrderState';
    $loop = 1;

    $apiUrl = "http://120.24.0.10/unitask/core/func.add.php?url=".$url."&start_date=".$start_date."&start_hour=".$start_hour."&start_minute=".$start_minute."&tag=".$tag."&loop=".$loop."";
    file_get_contents($apiUrl);
    // 定时器 end
    echo "<script>alert('发货成功');window.location.href='index.php?state=".$state."'</script>";
  }

// 查找快递公司
  $query = mysql_query("select * from express_delivery");
  while ($row=mysql_fetch_assoc($query)) {
    $express[]=$row;
  }
// 查找快递公司 end

// 填写回收地址 
if (isset($_POST['address'])) {
  $address = $_POST['address'];
  $ordersid = $_POST['ordersid'];
  $state2 = 2;
  $datetime = date("Y-m-d H:i:s");
  $query = mysql_query("UPDATE tuihuo set address='".$address."',state = ".$state2.",2time='".$datetime."' where ordersid=".$ordersid);
  echo "<script>alert('审核通过');window.location.href='index.php?state=4'</script>";
}
// 填写回收地址 end

// 商家拒绝退货 
if (isset($_POST['shop_comment'])) {
  if ($_FILES['shop_images']['name'][0] != '') {
    $UpFileTool=new UpFileTool('shop_images');
    $UpFileTool->multiImage($w=500, $h=500,$sumbimage='../sumbimage', $srcimage='../srcimage');
    $images=$UpFileTool->multiSumbImage;
    $srcimages=$UpFileTool->multiSrcImage;
  }

  $shop_comment = $_POST['shop_comment'];
  $ordersid = $_POST['ordersid'];
  $state3 = 3;
  $datetime = date("Y-m-d H:i:s");
  $query = mysql_query("UPDATE tuihuo set shop_comment='".$shop_comment."',shop_images='".$images."',shop_srcimages='".$srcimages."',state = ".$state3.",2time='".$datetime."' where ordersid=".$ordersid);
  echo "<script>alert('已拒绝接单');window.location.href='index.php?state=4'</script>";
}
// 商家拒绝退货 end

// 平台审核
if ($_REQUEST[act]=='checkpass') {
  $query = mysql_query("UPDATE tuihuo SET state=4, `3time`='".$time."' where ordersid=".$_REQUEST[ordersid]);
  echo "<script>window.location.href='index.php?state=4'</script>";
}elseif ($_REQUEST[act]=='checkfalse') {
  $query = mysql_query("UPDATE tuihuo SET state=5, `3time`='".$time."' where ordersid=".$_REQUEST[ordersid]);
  echo "<script>window.location.href='index.php?state=4'</script>";
}
// 平台审核 end
// 平台放款
if ($_REQUEST[act]=='payback') {
  // 更改退货表状态
  $query = mysql_query("UPDATE tuihuo set state=6, `4time`='".$time."'  where ordersid=".$_REQUEST[ordersid]);
  // 更改退货表状态 end
  
  // 根据单号查找订单信息
  $query = mysql_query("select * from goodsorders where ordersid='".$_REQUEST[ordersid]."'");
  $goodsorders = mysql_fetch_assoc($query);
  // 根据单号查找订单信息 end
  $query = mysql_query("select name from shop where shopid=".$goodsorders[shopid]."");
  $shop = mysql_fetch_assoc($query);
  // 记录消费者购买商品时商家的现金流量记录
  $content="消费者退货成功,商家".$shop[name]."销售收入减少".$goodsorders[totalprice]."元";
  $query = mysql_query("INSERT INTO cash_flow (amount,shopid,content) value (".-$goodsorders[totalprice].",".$goodsorders[shopid].",'".$content."')");
  // 记录消费者购买商品时商家的现金流量记录 end
  // 记录消费者购买商品时滞留在平台的现金流量记录
  $shopid=0; // shopid=0表示平台
  $content="消费者退货成功,商家".$shop[name]."滞留在平台的资金减少".$goodsorders[totalprice]."元";
  $query = mysql_query("INSERT INTO cash_flow (amount,shopid,fromshopid,content) value (".-$goodsorders[totalprice].",".$shopid.",".$goodsorders[shopid].",'".$content."')");
  // 记录消费者购买商品时滞留在平台的现金流量记录 end
  // 记录到现金流量表 end
  // 更新平台的资金
  $query = mysql_query("UPDATE admin set property=property-".$goodsorders[totalprice]);
  // 更新平台的资金 end
  // 更新商家的余额
  $query = mysql_query("UPDATE shop set balance=balance-".$goodsorders[totalprice]." where shopid=".$goodsorders[shopid]."");
  // 更新商家的余额 end
  echo "<script>window.location.href='index.php?state=4'</script>";
}
// 平台放款 end

// 取消退货 
if ($_REQUEST['act'] == 'cancel') {
  $query = mysql_query("select orderstate from tuihuo where ordersid=".$_REQUEST['ordersid']);
  $tuihuo = mysql_fetch_assoc($query);
  $orderstate = $tuihuo['orderstate'];

  $query = mysql_query("DELETE FROM tuihuo where ordersid=".$_REQUEST['ordersid']);
  $query = mysql_query("UPDATE goodsorders set state='".$orderstate."' where ordersid=".$_REQUEST['ordersid']);
  if (mysql_affected_rows()>0) {
    echo "<script>alert('取消退货成功');window.location.href='index.php?state=".$orderstate."'</script>";
  }else{
    echo "<script>alert('取消退货失败');window.location.href='index.php?state=".$orderstate."'</script>";
  }
}
// 取消退货 end

// 改变订单状态 
if ($_REQUEST['act'] == 'change') {
  $state = $_REQUEST['state'];
  $ordersid = $_REQUEST['ordersid'];
  $query = mysql_query("UPDATE goodsorders set state=".$state." where ordersid=".$ordersid);
  $url = 'index.php?state='.$state;
  if (mysql_affected_rows()>0) {
    echo "<script>alert('更新成功');window.location.href='".$url."'</script>";
  }else{
    echo "<script>alert('更新失败');window.location.href='".$url."'</script>";
  }
}
// 改变订单状态 end

// 改变退货状态 
if ($_REQUEST['act'] == 'change_tuihuo') {
  $state = $_REQUEST['state'];
  $ordersid = $_REQUEST['ordersid'];
  $query = mysql_query("UPDATE tuihuo set state=".$state." where ordersid=".$ordersid);
  $url = 'index.php?state=4';
  if (mysql_affected_rows()>0) {
    echo "<script>alert('更新成功');window.location.href='".$url."'</script>";
  }else{
    echo "<script>alert('更新失败');window.location.href='".$url."'</script>";
  }
}
// 改变退货状态 end
  ?>
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <link href="css/styles.css" rel="stylesheet" type="text/css" />
  <link href="css/style2.css" rel="stylesheet" type="text/css" />
  <script type="text/JavaScript">
    <!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body>

  <!--header-->
  <?php include("common/header.php");?>
  <!--header end-->


  <!-- staqrt subnav -->
  <div id="sub_nav">

    <div id="subnav_wrap" class="container_12">

      <!-- start sub nav list -->
      <div id="subnav" class=" grid_12">

        <ul>

          <li><a href="index.php" <?php if($_GET['state']==""){echo 'class="sub_nav_active"';}?>>所有订单</a></li>
          <li><a href="index.php?state=0" <?php if($_GET['state']=="0"){echo 'class="sub_nav_active"';}?>>待付款</a></li>
          <!-- <li><a href="index.php?state=1" <?php if($_GET['state']=="1"){echo 'class="sub_nav_active"';}?>>待商家接单</a></li> -->
          <li><a href="index.php?state=8" <?php if($_GET['state']=="8"){echo 'class="sub_nav_active"';}?>>待发货</a></li>
          <!-- <li><a href="index.php?state=9" <?php if($_GET['state']=="9"){echo 'class="sub_nav_active"';}?>>商家拒绝接单</a></li> -->
          <li><a href="index.php?state=2" <?php if($_GET['state']=="2"){echo 'class="sub_nav_active"';}?>>待客户收货</a></li>
          <li><a href="index.php?state=3" <?php if($_GET['state']=="3"){echo 'class="sub_nav_active"';}?>>待客户评价</a></li> 
          <li><a href="index.php?state=4" <?php if($_GET['state']=="4"){echo 'class="sub_nav_active"';}?>>退货售后</a></li>  
          <!-- <li><a href="index.php?state=5" <?php if($_GET['state']=="5"){echo 'class="sub_nav_active"';}?>>交易完成</a></li>     -->
          <!-- <li><a href="index.php?state=6" <?php if($_GET['state']=="6"){echo 'class="sub_nav_active"';}?>>交易关闭</a></li>     -->
        </ul>

      </div>
      <!-- end subnavigation list -->	

    </div>
  </div>
  </div>
  <!-- end sub_nav -->
  <div class="container_12">
    <form action="?del=ok" method="post">
      <!-- EVERYTING BELOW IS THE MAIN CONTENT -->

      <?php 
$state=3;
$perNumber=10; //每页显示的记录数
$page=$_GET['page']; //获得当前的页面值

if($_GET[state]!=""){
  $count=mysql_query("select count(*) from goodsorders where shopid = '".$_SESSION[shopid]."' and state ='".$_GET[state]."'");
}else{
  $count=mysql_query("select count(*) from goodsorders where shopid = '".$_SESSION[shopid]."'");
}

//获得记录总数
$rs=mysql_fetch_array($count); 
$totalNumber=$rs[0];
$totalPage=ceil($totalNumber/$perNumber); //计算出总页数

if (!isset($page)) {
  $page=1;
} //如果没有值,则赋值1
$startCount=($page-1)*$perNumber; //分页开始,根据此方法计算出开始的
if($_GET[state]!=""){
  $rert=mysql_query("select * from goodsorders where  state ='".$_GET[state]."' and shopid='".$_SESSION[shopid]."' order by ordersid desc limit $startCount,$perNumber"); 
}else{
  $rert=mysql_query("select * from goodsorders where shopid='".$_SESSION[shopid]."' order by ordersid desc limit $startCount,$perNumber"); 
}
while($sdg3=mysql_fetch_array($rert))
{	

  $count2=mysql_query("select count(*) from car where  ordersid ='".$sdg3[ordersid]."'");

//获得订单记录总数
  $rs2=mysql_fetch_array($count2);
  $totalNumber2=$rs2[0];
  ?>
  <div class="cy_mode" style="background: #fff; border:0px;">
    <input type="checkbox" name="checkbox[]" id="checkbox"  value="<?php echo $sdg3[ordersid];?>"/>  <strong>订单号：<?php echo $sdg3[numbers];?></strong>
    <a href="file2.php?id=<?php echo $sdg3[ordersid]; ?>&state=<?php echo $_REQUEST[state]; ?>" ><div class="dr_in">详情</div></a>
    <?php if($sdg3['state']=="1" || $sdg3['state']=="7"){?>
    <div class="dr_in accept" >接受订单</div>
    <div class="dr_in refuse" >拒绝订单</div>
    <?php }else if($sdg3['state']=="0"){?>
    <div class="dr_in2">等待客户付款</div>
    <?php }else if($sdg3['state']=="3"){?>
    <div class="dr_in2">等待客户评价</div>

    <?php }else if($sdg3['state']=="8"){?>
    <div class="dr_in delivery">发货</div>

    <?php }else if($sdg3['state']=="9"){?>
    <div class="dr_in2">等待平台处理</div>

    <?php }else if($sdg3['state']=="4"){?>
    <?php
      $sql = "select * from tuihuo where ordersid=".$sdg3[ordersid];
      $query = mysql_query($sql);
      $tuihuo = mysql_fetch_assoc($query);
      // var_dump($tuihuo);die;
      // 海淘
      if ($sdg3['type'] == 0) {
        if ($tuihuo[state] == 1) { ?>
          <?php if ($tuihuo['orderstate'] == 2 || $tuihuo['orderstate'] == 3) { ?>
            <div class="dr_in agree_return">同意退货</div>
          <?php }else{ ?>
            <div class="dr_in agree">同意退货</div>
          <?php } ?>
        <div class="dr_in refuse_return">拒绝退货</div>

        <?php }elseif ($tuihuo[state]==2) { ?>

          <?php if ($tuihuo[orderstate]==2 || $tuihuo[orderstate]==3) { ?>
            <div class="dr_in2">待用户退货</div>
          <?php }else{ ?>
            <div class="dr_in2">等待平台处理</div>
          <?php } ?>

        <?php }elseif ($tuihuo[state]==3) { ?>
        <div class="dr_in2">商家拒绝退货</div>

        <?php }elseif ($tuihuo[state]==4) { ?>
         <div class="dr_in recover">确认回收</div>
        <?php }elseif ($tuihuo[state]==5) { ?>
        <div class="dr_in2">等待平台处理</div>
        <?php }elseif ($tuihuo[state]==6) { ?>
        <div class="dr_in2">退货完成</div>
        <?php } 
      // 快购
      }elseif ($sdg3['type'] == 1) {
        if ($tuihuo[state]==1) { ?>
        <div class="dr_in2">等待平台处理</div>
        <?php } 
      }
?>
    <?php }else if($sdg3['state']=="5"){?>
    <div class="dr_in2">交易完成</div>
    <?php }else if($sdg3['state']=="6"){?>
    <div class="dr_in2">交易关闭</div>
    <?php }else{?>
    <div class="dr_in2">待客户收货</div>
    <?php }?>

  </div>
  <?php 
  $state=1;
  $rert2=mysql_query("select * from car where  ordersid ='".$sdg3[ordersid]."' order by carid desc"); 
  while($sdg2=mysql_fetch_array($rert2))

  {


    $rert4=mysql_query("select * from goods where  goodsid ='".$sdg2[goodsid]."'"); 
    $sdg=mysql_fetch_array($rert4);

    $ime=$sdg[images];
    $de = array();
    $de=explode("," ,$ime);
    $images=$de[0];

    ?>
    <div class="cy_mode" style="background:#FFFFFF; margin-top:0px;">
      <div class="dr_mimg">
        <img src="../<?php echo $images; ?>" width="50%"  height="auto">
      </div>
      <div class="dr_conm">

        <strong><?php echo $sdg[title]; ?></strong><span class="conf_wd all_fr">￥<?php echo $sdg2[price]; ?></span>
        <br/>
        <span class="conf_wd">颜色：<?php if($sdg2[chicun]!=""){echo $sdg2[chicun],",",$sdg2[caizhi];} ?></span><span class="conf_wd all_fr">x<?php echo $sdg2[number]; ?></span>
        <br/>
        <span class="conf_wd all_fr">总计：￥<?php echo ($sdg2[price]*$sdg2[number]); ?></span>
        总计：<?php echo $totalNumber2; ?>件产品
         <br/>
        <span class="conf_wd all_fr">用户留言：<b><?php echo $sdg3[msg]; ?></b></span>
         
      </div>

    </div>


    <?php }}?>
    <div class="grid_12">

      <div class="clearfix"><input name="" type="submit"  value="删除选中"  class="submit" /></div>

      <ul id="pagination">



        <?php  

        if ($page > 1) { //上一页 ?>
        <li><a     href="?page=1&state=<?php echo $_REQUEST[state]; ?>">首页</a></li>
        <li><a     href="?page=<?php echo ($page-1); ?>&state=<?php echo $_REQUEST[state]; ?>">上一页</a></li>
        <?php }?>

        <li class="active"><?php echo $page;?></li>

        <?php



        if ($_GET[page]!=$totalPage) { //下一页 ?>
        <li><a     href="?page=<?php echo ($page+1); ?>&state=<?php echo $_REQUEST[state]; ?>">下一页</a></li>
        <?php }

        if ($totalPage>1) { //下一页 ?>
        <li><a     href="?page=<?php echo $totalPage; ?>&state=<?php echo $_REQUEST[state]; ?>">尾页</a></li>

        <?php }?>

        总共有<?php echo $totalNumber;?>个订单 
      </ul>

    </div>           
  </div>
  <!-- end content wrap -->

</form>
</div>
<!-- 遮罩层 -->
<div class="mask_div">
</div>
<!-- 遮罩层 end -->
<!-- 填写快递公司及快递单号  -->
<div class="alert_div">
  <div class="alert_title">
    填写快递单号
  </div> 
  <div class="alert_line"></div>
  <div class="alert_body">
    <form id="alert_form" action="" method="post">
      <label>快递公司:</label>
      <select name="kuaidi" class="select">
        <?php foreach ($express as $v) { ?>
        <option value="<?php echo $v[id]; ?>"><?php echo $v[name] ?></option>
        <?php } ?>
      </select>
      <label>快递单号:</label><input type="text" class="text" name="danhao" value="">
      <input type="hidden" name="ordersid" id="ordersid" value="">
    </form>
  </div>
  <div class="alert_foot">
    <div class="foot-div">
      <button type="button" class="submit" id="submit1">提交</button>
      <button type="button" class="submit" id="cancel1">取消</button>
    </div>
  </div>
</div>
<!-- 填写快递公司及快递单号 end -->

<!-- 商家填写回收货物地址  -->
<div class="alert_div1">
  <div class="alert_title">
    填写回收地址
  </div> 
  <div class="alert_line"></div>
  <div class="alert_body">
    <form id="alert_form1" action="" method="post">
      <label>回收地址:</label>
      <input type="text" class="text" name="address" value="">
      <input type="hidden" name="ordersid" id="ordersid1" value="">
    </form>
  </div>
  <div class="alertfa_foot">
    <div class="foot-div">
      <button type="button" class="submit" id="submit2">提交</button>
      <button type="button" class="submit" id="cancel2">取消</button>
    </div>
  </div>
</div>
<!-- 商家填写回收货物地址 end -->

<!-- 商家拒绝退货  -->
<div class="alert_div2">
  <div class="alert_title">
    填写拒绝退货原因
  </div> 
  <div class="alert_line"></div>
  <div class="alert_body">
    <form id="alert_form2" action="" method="post" enctype="multipart/form-data">
      <label>拒绝说明:</label>
      <textarea name="shop_comment" style="height:150px;width: 300px;">
      </textarea>

      <label>说明附图:</label>
      <input type="file" class="text" name="shop_images[]" multiple>
      <input type="hidden" name="ordersid" id="ordersid2" value="">
    </form>
  </div>
  <div class="alert_foot">
    <div class="foot-div">
      <button type="button" class="submit" id="submit3">提交</button>
      <button type="button" class="submit" id="cancel3">取消</button>
    </div>
  </div>
</div>
<!-- 商家拒绝退货 end -->
</body>
<script type="text/javascript">

  $('#submit1').click(function(){
    $('#alert_form').submit();
    $('.mask_div').hide();
    $('.alert_div').hide();
  });
  $('#cancel1').click(function(){
    $('.mask_div').hide();
    $('.alert_div').hide();
  })

  $('#submit2').click(function(){
    $('#alert_form1').submit();
    $('.mask_div').hide();
    $('.alert_div1').hide();
  });
  $('#cancel2').click(function(){
    $('.mask_div').hide();
    $('.alert_div1').hide();
  })

  $('#submit3').click(function(){
    $('#alert_form2').submit();
    $('.mask_div').hide();
    $('.alert_div2').hide();
  });
  $('#cancel3').click(function(){
    $('.mask_div').hide();
    $('.alert_div2').hide();
  })

  // 改变订单状态
  function change_orders(ordersid,state){
    var url = 'index.php?act=change&ordersid='+ordersid+'&state='+state;
    window.location.href = url;
  }
  // 改变订单状态 end

  // 改变退货状态
  function change_tuihuo(ordersid,state){
    var url = 'index.php?act=change_tuihuo&ordersid='+ordersid+'&state='+state;
    window.location.href = url;
  }
  // 改变退货状态 end

  // 接受订单 
  $('.accept').click(function(){
    var ordersid=$(this).parent().find('#checkbox').val();
    var state = 8;
    change_orders(ordersid,state);
  })
  // 接受订单 end

  // 发货 
  $('.delivery').click(function(){
    var ordersid=$(this).parent().find('#checkbox').val();
    $('#ordersid').attr('value',ordersid);
    $('.mask_div').show();
    $('.alert_div input[type="text"]').attr('value','');
    $('.alert_div').show();
  })
  // 发货 end

  // 同意退货(商家已发货) 
  $('.agree_return').click(function(){
    alert(1);
    var ordersid=$(this).parent().find('#checkbox').val();
    $('#ordersid1').attr('value',ordersid);
    $('.mask_div').show();
    $('.alert_div1 input[type="text"]').attr('value','');
    $('.alert_div1').show();
  })
  // 同意退货(商家已发货) end

  // 同意退货(商家未发货) 
  $('.agree').click(function(){
    var ordersid=$(this).parent().find('#checkbox').val();
    var state = 5;
    change_tuihuo(ordersid,state);
  })
  // 同意退货(商家未发货) end

  // 拒绝退货 
  $('.refuse_return').click(function(){
    var ordersid=$(this).parent().find('#checkbox').val();
    $('#ordersid2').attr('value',ordersid);
    $('.mask_div').show();
    $('.alert_div2 input[type="text"]').attr('value','');
    $('.alert_div2').show();
  })
  // 拒绝退货 end

  // 商家确认回收 
  $('.recover').click(function(){
    var ordersid=$(this).parent().find('#checkbox').val();
    var state = 5;
    change_tuihuo(ordersid,state);
  })
  // 商家确认回收 end
</script>
</html>
